<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>媒体相册 Gallery Form</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all"/>
    <style type="text/css">
        html{
            background-color: #FFF;
        }
        .pic {
            margin-left: 130px;
        }

        .pic dl {
            float: left;
            position: relative;
        }

        .pic dl dd {
            position: absolute;
            right: 5px;
            top: 5px;
            cursor: pointer;
            background: #666;
            color: #fff;
            padding: 2px;
        }

        .pic img {
            height: 100px;
            margin: 5px 0;
            margin-right: 5px;
        }

    </style>
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-form-gallery" id="layuiadmin-form-gallery" style="padding: 20px 30px 0 0;">
    <div class="layui-form-item" th:each="nv:${nameVos}">
        <label class="layui-form-label">[[${nv.name}]]名称</label>
        <div class="layui-input-block">
            <input type="text" th:name="${nv.code}" lay-verify="required" lay-data="name" autocomplete="off" th:placeholder="${'请输入'+ nv.name +'名称'}"
                   class="layui-input" th:value="${nv.value}">
        </div>
    </div>
    <div class="layui-form-item" th:if="${type == 'image' || (gallery != null && gallery.type == 'image')}">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-inline">
            <input type="text" name="image" id="image" placeholder="请上传图片" class="layui-input" th:value="${gallery.url}">
        </div>
        <button type="button" class="layui-btn upload upload-pic" data-des="image">
            <i class="layui-icon">&#xe67c;</i>上传图片
        </button>
        <div class="pic pic_box">
            <dl th:if="${gallery.url}">
                <dt><img th:src="${gallery.url}"
                         th:data-url="${gallery.url}"></dt>
            </dl>
        </div>
    </div>
    <div class="layui-form-item" th:if="${type == 'video' || (gallery != null && gallery.type == 'video')}">
        <label class="layui-form-label">视频</label>
        <div class="layui-input-inline">
            <input type="text" name="video" id="video" placeholder="请上传视频" class="layui-input" th:value="${gallery.url}">
        </div>
        <button type="button" class="layui-btn upload upload-video" data-des="video">
            <i class="layui-icon">&#xe67c;</i>上传视频
        </button>
    </div>
    <div class="layui-form-item" th:if="${type == 'directory' || type == 'video' || (gallery != null && (gallery.type == 'directory' || gallery.type == 'video'))}">
        <label class="layui-form-label">封面</label>
        <div class="layui-input-inline">
            <input type="text" name="preview" id="preview" placeholder="请上传封面" class="layui-input" th:value="${gallery.preview}">
        </div>
        <button type="button" class="layui-btn upload upload-pic" data-des="preview">
            <i class="layui-icon">&#xe67c;</i>上传封面
        </button>
        <div class="pic pic_box">
            <dl th:if="${gallery.preview}">
                <dt><img th:src="${gallery.preview}"
                         th:data-url="${gallery.preview}"></dt>
            </dl>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" th:checked="${gallery.deleted eq 0}" name="deleted" id="LAY-gallery-deleted" lay-skin="switch"/>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="hidden" name="id" th:value="${gallery.id == null ? '' : gallery.id }"/>
        <input type="hidden" name="pid" th:value="${gallery.pid == null ? pid : gallery.pid}"/>
        <input type="hidden" name="type" th:value="${gallery.type == null ? type : gallery.type}"/>
        <input type="hidden" name="size" th:value="${gallery.size == null ? '' : gallery.size}"/>
        <input type="hidden" name="path" th:value="${gallery.path == null ? path : gallery.path}"/>
        <button class="layui-btn" lay-submit lay-filter="LAY-gallery-add-submit" id="LAY-gallery-add-submit">提交</button>
    </div>
</div>

<script th:src="@{/statics/layui/layui.js}"></script>
<script>
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'upload',  'form'], function () {
        var $ = layui.$
            , form = layui.form
            , upload = layui.upload;

        upload.render({
            elem: '.upload-pic'
            , url: '/api/file/aliyun/upload'
            , accept: 'images'
            , method: 'post'
            , multiple: false //多文件上传
            , acceptMime: 'image/*'
            ,before: function(obj){
                layer.load(); //上传loading
            }
            ,done: function(res){
                var item = this.item;
                var des=$(item).data('des');
                console.log(des);
                layer.closeAll('loading'); //关闭loading
                if(res.code==0){
                    $('#'+des).val(res.data);
                    layer.msg('上传成功！');
                }else{
                    layer.msg('上传失败！');
                }
            }
            ,error: function(){
                layer.closeAll('loading'); //关闭loading
                layer.msg('上传发生错误！');
            }
        });

        upload.render({
            elem: '.upload-video'
            , url: '/api/file/aliyun/upload'
            , accept: 'video'
            , method: 'post'
            , acceptMime: 'video/*'
            , multiple: false //多文件上传
            ,before: function(obj){
                layer.load(); //上传loading
            }
            ,done: function(res){
                var item = this.item;
                var des=$(item).data('des');
                console.log(des);
                layer.closeAll('loading'); //关闭loading
                if(res.code==0){
                    $('#'+des).val(res.data);
                    layer.msg('上传成功！');
                }else{
                    layer.msg('上传失败！');
                }
            }
            ,error: function(){
                layer.closeAll('loading'); //关闭loading
                layer.msg('上传发生错误！');
            }
        });

    })
</script>
</body>
</html>